
<div class="amily2-header">
    <div id="amily2_open_hanlin_tutorial" class="additional-features-title interactable" title="查看翰林院使用教程" style="cursor: pointer;">
        <i class="fas fa-landmark-dome"></i> 翰林院 · 忆识核心
    </div>
    <button id="amily2_back_to_main_from_hanlinyuan" class="menu_button secondary small_button interactable">
        返回主殿 <i class="fas fa-arrow-right"></i>
    </button>
</div>
<hr class="header-divider">

<div id="hly-modal-container">
    <!-- 状态诏书 -->
    <div class="hly-imperial-edict">
        <fieldset class="hly-settings-group" style="margin-bottom: 10px;">
            <legend><i class="fas fa-power-off"></i> 总开关</legend>
            <div class="hly-control-block" style="flex-direction: row; justify-content: space-between; align-items: center;">
                <label for="hly-retrieval-enabled-toggle">开启忆识检索之权</label>
                <label class="hly-toggle-switch">
                    <input type="checkbox" id="hly-retrieval-enabled" data-setting-key="retrieval.enabled" data-type="boolean">
                    <span class="slider"></span>
                </label>
            </div>
        </fieldset>
        <div class="hly-edict-row">
            <div class="hly-edict-item">
                <span class="hly-edict-label">当前会话:</span>
                <span id="hly-current-chat-id" class="hly-edict-value">未开启</span>
            </div>
            <div class="hly-edict-item">
                <span class="hly-edict-label">当前辅佐:</span>
                <span id="hly-current-character-name" class="hly-edict-value">待命中...</span>
            </div>
        </div>
        <div class="hly-edict-row">
            <div class="hly-edict-item hly-memory-display">
                <span class="hly-edict-label">忆识总数:</span>
                <span id="hly-current-vector-count" class="hly-memory-count">0</span>
                <button class="hly-action-button" onclick="updateHLYMemoryCount()" title="刷新忆识总数" style="padding: 4px 8px; font-size: 12px;">
                    🔄
                </button>
            </div>
            <div class="hly-button-group" style="margin-left: auto;">
                <button class="hly-action-button" onclick="purgeHLYStorage()" style="padding: 4px 8px; font-size: 12px;">清空宝库</button>
                <button id="hly-session-lock-btn" class="hly-action-button" style="padding: 4px 8px; font-size: 12px;">
                    <i class="fas fa-lock"></i> <span>锁定会话</span>
                </button>
            </div>
        </div>
    </div>

    <!-- 司南 -->
    <div class="hly-navigation-deck">
        <button class="hly-nav-item active" data-tab="retrieval">忆识检索</button>
        <button class="hly-nav-item" data-tab="historiography">书库编纂</button>
        <button class="hly-nav-item" data-tab="knowledge-bases">知识管理</button>
        <button class="hly-nav-item" data-tab="rerank">忆识精炼</button>
        <button class="hly-nav-item" data-tab="advanced">高级设定</button>
    </div>

    <div class="hly-scroll">
        <div id="hly-retrieval-tab" class="hly-tab-pane active">
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-broadcast-tower"></i> 神力之源 (API)</legend>
                <div class="hly-control-block">
                    <label for="hly-api-endpoint">API模式:</label>
                                <select id="hly-api-endpoint" class="hly-imperial-brush" data-setting-key="retrieval.apiEndpoint" data-type="string">
                                    <option value="custom">自定义 (OpenAI/Azure 兼容)</option>
                                    <option value="google_direct">Google 直连</option>
                                    <option value="local_proxy">本地代理 (LM Studio/Ollama)</option>
                                </select>
                </div>
                <div class="hly-control-block" id="hly-custom-endpoint-docket">
                    <label for="hly-custom-api-url">API URL:</label>
                    <input type="text" id="hly-custom-api-url" class="hly-imperial-brush" placeholder="根据所选模式填写" data-setting-key="retrieval.customApiUrl" data-type="string">
                </div>
                <div class="hly-control-block" id="hly-api-key-group">
                    <label for="hly-api-key">通行令牌 (API Key):</label>
                    <input type="password" id="hly-api-key" class="hly-imperial-brush" placeholder="请在此输入您的通行令牌" data-setting-key="retrieval.apiKey" data-type="string">
                </div>
                <div class="hly-control-block">
                    <label for="hly-embedding-model">嵌入模型:</label>
                    <select id="hly-embedding-model" class="hly-imperial-brush" data-setting-key="retrieval.embeddingModel" data-type="string"></select>
                </div>
                 <div class="hly-button-group">
                    <button class="hly-action-button" onclick="testHLYApi()">测试神力</button>
                    <button class="hly-action-button" onclick="fetchHLYEmbeddingModels()">获取模型</button>
                    <button class="hly-action-button danger" onclick="resetHLYSettings()">重置为初</button>
                </div>
            </fieldset>
        </div>


        <div id="hly-rerank-tab" class="hly-tab-pane">
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-wand-magic-sparkles"></i> Rerank 精炼</legend>
                <div class="hly-control-block" style="flex-direction: row; justify-content: space-between; align-items: center;">
                    <label for="hly-rerank-enabled-toggle">启用 Rerank</label>
                    <label class="hly-toggle-switch">
                        <input type="checkbox" id="hly-rerank-enabled" data-setting-key="rerank.enabled" data-type="boolean">
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="hly-control-block">
                    <label for="hly-rerank-api-mode">Rerank API 模式:</label>
                    <select id="hly-rerank-api-mode" class="hly-imperial-brush" data-setting-key="rerank.apiMode" data-type="string">
                        <option value="custom">自定义 (兼容API)</option>
                        <option value="local_proxy">本地代理</option>
                    </select>
                </div>
                <div class="hly-control-block">
                    <label for="hly-rerank-url">Rerank API 地址:</label>
                    <input type="text" id="hly-rerank-url" class="hly-imperial-brush" placeholder="根据所选模式填写" data-setting-key="rerank.url" data-type="string">
                </div>
                <div class="hly-control-block" id="hly-rerank-api-key-group">
                    <label for="hly-rerank-api-key">Rerank API Key:</label>
                    <input type="password" id="hly-rerank-api-key" class="hly-imperial-brush" placeholder="请输入您的 Rerank API Key" data-setting-key="rerank.apiKey" data-type="string">
                </div>
                <div class="hly-control-block">
                    <label for="hly-rerank-model">Rerank 模型:</label>
                    <div style="display: flex; width: 100%;">
                        <select id="hly-rerank-model" class="hly-imperial-brush" data-setting-key="rerank.model" data-type="string" style="flex-grow: 1;"></select>
                        <button class="hly-action-button" onclick="fetchHLYRerankModels()" style="margin-left: 10px; flex-shrink: 0;">获取模型</button>
                    </div>
                </div>
                <div class="hly-control-block">
                    <label for="hly-rerank-top-n">返回结果数 (top_n):</label>
                    <input type="number" id="hly-rerank-top-n" class="hly-imperial-brush" value="5" data-setting-key="rerank.top_n" data-type="integer">
                </div>
                <div class="hly-control-block">
                    <label for="hly-rerank-hybrid-alpha">混合分数权重 (Alpha):</label>
                    <input type="number" id="hly-rerank-hybrid-alpha" class="hly-imperial-brush" value="0.7" step="0.1" min="0" max="1" data-setting-key="rerank.hybrid_alpha" data-type="float">
                    <small class="hly-notes">Rerank分数权重。1.0表示只用Rerank分数，0.0表示只用原始相似度分数。</small>
                </div>
                <div class="hly-control-block" style="flex-direction: row; justify-content: space-between; align-items: center;">
                    <label for="hly-rerank-notify-toggle">Rerank 时上奏</label>
                    <label class="hly-toggle-switch">
                        <input type="checkbox" id="hly-rerank-notify" data-setting-key="rerank.notify" data-type="boolean">
                        <span class="slider"></span>
                    </label>
                </div>
            </fieldset>
        </div>

        <div id="hly-historiography-tab" class="hly-tab-pane">

            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-book-medical"></i> 凝识法则</legend>
                 <div class="hly-control-block" style="flex-direction: row; justify-content: space-between; align-items: center;">
                    <label for="hly-condensation-enabled-toggle">准许凝识</label>
                    <label class="hly-toggle-switch">
                        <input type="checkbox" id="hly-condensation-enabled" data-setting-key="condensation.enabled" data-type="boolean">
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="hly-control-block">
                    <label>凝识范围 (聊天楼层):</label>
                    <div style="display: flex; gap: 10px; align-items: center;">
                        <input type="number" id="hly-layer-start" class="hly-imperial-brush" value="1" data-setting-key="condensation.layerStart" data-type="integer">
                        <span>-</span>
                        <input type="number" id="hly-layer-end" class="hly-imperial-brush" value="10" data-setting-key="condensation.layerEnd" data-type="integer">
                    </div>
                </div>
                <div class="hly-control-block">
                    <label class="hly-label">消息来源:</label>
                    <div class="hly-checkbox-group">
                        <label><input type="checkbox" id="hly-include-user" data-setting-key="condensation.messageTypes.user" data-type="boolean"> 用户</label>
                        <label><input type="checkbox" id="hly-include-ai" data-setting-key="condensation.messageTypes.ai" data-type="boolean"> AI</label>
                    </div>
                </div>

                <div class="hly-control-block" style="flex-direction: row; justify-content: space-between; align-items: center;">
                    <label for="hly-tag-extraction-toggle">标签提取</label>
                    <label class="hly-toggle-switch">
                        <input type="checkbox" id="hly-tag-extraction-toggle" data-setting-key="condensation.tagExtractionEnabled" data-type="boolean">
                        <span class="slider"></span>
                    </label>
                </div>
                <div id="hly-tag-input-container" class="hly-control-block" style="display: none;">
                    <label for="hly-tag-input">输入标签 (以逗号分隔):</label>
                    <textarea id="hly-tag-input" class="hly-imperial-brush" rows="2" placeholder="例如: content,details,摘要" data-setting-key="condensation.tags" data-type="string"></textarea>
                </div>
                <div class="hly-button-group" style="justify-content: flex-start;">
                    <button id="hly-exclusion-rules-btn" class="hly-action-button">内容排除</button>
                </div>
                <div class="hly-button-group">
                    <button class="hly-action-button success" onclick="startHLYCondensation()"> 开始凝识</button>
                    <button class="hly-action-button" onclick="previewHLYCondensation()"> 预览内容</button>
                </div>
                <div class="hly-control-block">
                    <label>凝识结果预览:</label>
                    <div id="hly-condensation-results" class="hly-results-display"></div>
                </div>
            </fieldset>

            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-book"></i> 手动录入</legend>
                <div class="hly-control-block">
                    <label for="hly-manual-text">在此处粘贴您要录入的知识文书：</label>
                    <textarea id="hly-manual-text" class="hly-imperial-brush" rows="8" placeholder="例如，您可以粘贴角色设定、世界观、背景故事等..."></textarea>
                    <small class="hly-notes">录入的文本将被分块、向量化并存入当前角色的忆识宝库中。</small>
                </div>
                <div class="hly-button-group">
                    <button class="hly-action-button success" onclick="ingestHLYManualText()">
                        <i class="fas fa-file-import"></i> 开始录入
                    </button>
                </div>
            </fieldset>

            <!-- ================== 小说/文档导入模块 ================== -->
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-book-open"></i> 整本录入 (小说/文档)</legend>
                <div class="hly-control-block">
                    <label for="hanlin_novel_uploader">选择一个 .txt 格式的文本文档：</label>
                    <div id="hanlinyuan-ingest-novel-controls" class="hly-button-group" style="align-items: center;">
                        <!-- 文件选择按钮 -->
                        <div class="file-input-container">
                            <label for="hanlinyuan-ingest-novel-file-input" class="hly-action-button">选择.txt文件</label>
                            <input type="file" id="hanlinyuan-ingest-novel-file-input" accept=".txt" style="display: none;">
                        </div>
                        <!-- 编码选择 -->
                        <div class="file-encoding-container">
                            <select id="hanlinyuan-ingest-novel-encoding" class="hly-imperial-brush" style="padding: 8px; height: auto;" title="选择您原始文件的编码格式，系统将为您转换为标准的UTF-8格式进行处理。">
                                <option value="UTF-8" selected>UTF-8 (默认)</option>
                                <option value="GBK">GBK/GB2312 → UTF-8</option>
                                <option value="Big5">Big5 → UTF-8</option>
                            </select>
                        </div>
                        <!-- 开始按钮 -->
                        <button id="hanlinyuan-ingest-novel-start" class="hly-action-button success">开始录入</button>
                    </div>
                     <span id="hanlinyuan-ingest-novel-file-name" class="file-name" style="margin-top: 10px; color: #ccc; display: inline-block;">未选择文件</span>

                    <div id="hanlinyuan-ingest-progress-container" style="display: none; margin-top: 10px;">
                        <div id="hanlinyuan-ingest-status" style="margin-bottom: 5px;">正在准备...</div>
                        <div style="display: flex; align-items: center;">
                            <progress id="hanlinyuan-ingest-progress-bar" value="0" max="100" style="width: 100%;"></progress>
                            <button id="hanlinyuan-ingest-abort" class="hly-action-button danger" style="margin-left: 10px;">中止</button>
                        </div>
                    </div>
                    <small class="hly-notes" style="margin-top: 10px;">上传 .txt 文件，系统会自动分块并存入忆识核心。处理大文件时请耐心等待。</small>
                </div>
            </fieldset>


            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-list-alt"></i> 按条目编纂</legend>
                <div class="hly-control-block">
                    <label for="hly-hist-select-library">选择书库:</label>
                    <div class="hly-search-wrapper">
                        <input type="text" id="hly-worldbook-search" class="hly-search-input" placeholder="搜索世界书名称...">
                        <i class="fas fa-search hly-search-icon"></i>
                    </div>
                    <select id="hly-hist-select-library" class="hly-imperial-brush">
                        <option value="">请选择书库...</option>
                    </select>
                </div>
                <div class="hly-control-block">
                    <label for="hly-hist-select-entry">选择条目:</label>
                    <div class="hly-multiselect-container">
                        <div class="hly-entry-search-wrapper">
                            <input type="text" id="hly-entry-search" class="hly-search-input" placeholder="搜索条目名称、关键词...">
                            <i class="fas fa-search hly-search-icon"></i>
                        </div>
                        <button id="hly-hist-entry-multiselect-btn" class="hly-imperial-brush" disabled>
                            <span>请先选择书库...</span>
                            <i class="fas fa-chevron-down"></i>
                        </button>
                        <div id="hly-hist-entry-multiselect-options" class="hly-multiselect-options-container" style="display: none;">
                            <!-- Options will be populated by JS -->
                        </div>
                    </div>
                </div>
                <div class="hly-button-group">
                    <button class="hly-action-button success" onclick="startHLYHistoriography()">
                        <i class="fas fa-file-import"></i> 开始编纂
                    </button>
                </div>

                <div class="hly-control-block">
                    <label>编纂结果预览:</label>
                    <div id="hly-historiography-results" class="hly-results-display"></div>
                </div>
            </fieldset>
        </div>


        <!-- ================== Knowledge Base Management Tab ================== -->
        <div id="hly-knowledge-bases-tab" class="hly-tab-pane">
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-globe"></i> 全局知识库 (全角色通用)</legend>
                <div id="hly-kb-list-global" class="hly-kb-list">
                    <p id="hly-kb-list-global-placeholder" style="color: #888;">尚无全局知识库。</p>
                    <!-- Global KBs will be populated here -->
                </div>
            </fieldset>

            <div class="hly-kb-transfer-controls">
                <button id="hly-kb-move-all-to-local" class="hly-action-button" title="将所有全局知识库移动到当前角色的局部列表">
                    <i class="fas fa-angles-down"></i> 全局移至局部
                </button>
                <button id="hly-kb-move-all-to-global" class="hly-action-button" title="将当前角色的所有局部知识库移动到全局列表">
                    <i class="fas fa-angles-up"></i> 局部移至全局
                </button>
            </div>

            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-user"></i> <span id="hly-local-kb-char-name">当前角色</span>的局部知识库</legend>
                <div id="hly-kb-list-local" class="hly-kb-list">
                    <p id="hly-kb-list-local-placeholder" style="color: #888;">当前角色没有知识库。通过“书库编纂”中的功能可自动创建。</p>
                    <!-- Local KBs will be populated here -->
                </div>
                <div class="hly-button-group" style="margin-top: 15px;">
                    <button id="hly-kb-delete-local-btn" class="hly-action-button danger">
                        <i class="fas fa-skull-crossbones"></i> 删除该角色所有局部知识库
                    </button>
                </div>
            </fieldset>
        </div>


        <div id="hly-advanced-tab" class="hly-tab-pane">
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-cogs"></i> 检索微调</legend>
                <div class="hly-control-block">
                    <label for="hly-chunk-size">书卷尺寸 (Chunk Size):</label>
                    <input type="number" id="hly-chunk-size" class="hly-imperial-brush" value="768" data-setting-key="advanced.chunkSize" data-type="integer">
                </div>
                <div class="hly-control-block">
                    <label for="hly-overlap-size">上下文关联度 (Overlap):</label>
                    <input type="number" id="hly-overlap-size" class="hly-imperial-brush" value="50" data-setting-key="advanced.overlap" data-type="integer">
                </div>
                <div class="hly-control-block">
                    <label for="hly-match-threshold">忆识匹配度 (Threshold):</label>
                    <input type="number" id="hly-match-threshold" class="hly-imperial-brush" value="0.5" step="0.1" data-setting-key="advanced.matchThreshold" data-type="float">
                </div>
                <div class="hly-control-block">
                    <label for="hly-query-message-count">检索参考的消息数量:</label>
                    <input type="number" id="hly-query-message-count" class="hly-imperial-brush" value="2" data-setting-key="advanced.queryMessageCount" data-type="integer">
                </div>
                <div class="hly-control-block">
                    <label for="hly-max-results">单次检索最大结果数:</label>
                    <input type="number" id="hly-max-results" class="hly-imperial-brush" value="10" data-setting-key="advanced.maxResults" data-type="integer">
                </div>
                <div class="hly-control-block">
                    <label for="hly-batch-size">批处理大小 (Batch Size):</label>
                    <input type="number" id="hly-batch-size" class="hly-imperial-brush" value="50" data-setting-key="retrieval.batchSize" data-type="integer">
                    <small class="hly-notes">每次调用API时处理的文本数量。</small>
                </div>
            </fieldset>
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-wand-magic-sparkles"></i> 圣言注入</legend>
                <div class="hly-control-block">
                    <label for="hly-injection-template">圣言模板:</label>
                    <textarea id="hly-injection-template" class="hly-imperial-brush" rows="3" data-setting-key="injection.template" data-type="string">翰林院呈报相关忆识：\n{{text}}</textarea>
                    <small class="hly-notes">以 {{text}} 为占位符，代表检索到的内容。</small>
                </div>


                <div class="hly-control-block">
                    <label>注入位置:</label>
                    <div class="hly-radio-group-vertical">
                        <label class="hly-radio-label">
                            <input type="radio" name="hly-injection-position" value="2" data-setting-key="injection.position" data-type="integer" />
                            <span>主提示前 (在所有提示词的最前面)</span>
                        </label>
                        <label class="hly-radio-label">
                            <input type="radio" name="hly-injection-position" value="0" data-setting-key="injection.position" data-type="integer" />
                            <span>主提示后 (在主提示词之后，聊天记录之前)</span>
                        </label>
                        <label class="hly-radio-label">
                            <input type="radio" name="hly-injection-position" value="1" checked data-setting-key="injection.position" data-type="integer" />
                            <span>聊天内 @ 深度</span>
                            <input
                                id="hly-injection-depth"
                                class="hly-imperial-brush"
                                type="number"
                                min="0"
                                max="999"
                                style="width: 60px; margin-left: 10px;"
                                data-setting-key="injection.depth" data-type="integer"
                            />
                            <span style="margin-left: 10px;">作为</span>
                            <select id="hly-injection-role" class="hly-imperial-brush" style="width: auto; margin-left: 10px;" data-setting-key="injection.depth_role" data-type="integer">
                                <option value="0">系统</option>
                                <option value="1">用户</option>
                                <option value="2">助手</option>
                            </select>
                        </label>
                    </div>
                </div>
            </fieldset>
            <fieldset class="hly-settings-group">
                <legend><i class="fas fa-bell"></i> 上奏设定</legend>
                <div class="hly-control-block" style="flex-direction: row; justify-content: space-between; align-items: center;">
                    <label for="hly-retrieval-notify-toggle">检索成功时上奏</label>
                    <label class="hly-toggle-switch">
                        <input type="checkbox" id="hly-retrieval-notify" data-setting-key="retrieval.notify" data-type="boolean">
                        <span class="slider"></span>
                    </label>
                </div>
            </fieldset>
        </div>
    </div>


    <div id="hly-log-container" style="border-top: 2px solid #444; padding-top: 10px; margin-top: 10px;">
        <fieldset class="hly-settings-group">
            <legend><i class="fas fa-scroll"></i> 起居注</legend>
            <div id="hly-log-output" class="hly-log-display">
                <p>翰林院运行日志将在此记录...</p>
            </div>
        </fieldset>
    </div>

    <div class="hly-footer">
    </div>
</div>
<!-- 引入强大的文本解码库，确保编码转换万无一失 -->
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
